package com.jintian.smart.platform.system.service;

import com.jintian.smart.kernel.common.util.DateUtil;
import com.jintian.smart.kernel.common.util.SmartBeanUtil;
import com.jintian.smart.kernel.common.util.StringUtil;
import com.jintian.smart.kernel.core.domain.RequestUser;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.base.api.system.entity.FeedbackEntity;
import com.jintian.smart.base.api.system.form.FeedbackAddForm;
import com.jintian.smart.base.api.system.form.FeedbackQueryForm;
import com.jintian.smart.base.api.system.view.FeedbackVO;
import com.jintian.smart.platform.system.mapper.FeedbackMapper;
import jakarta.annotation.Resource;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.stereotype.Service;


/**
 * 意见反馈
 */
@Service
public class FeedbackService {
    @Resource
    private FeedbackMapper feedbackMapper;

    /**
     * 分页查询
     */
    public ResponseDTO<PageResult<FeedbackVO>> query(FeedbackQueryForm queryForm) {
        LambdaQuery<FeedbackEntity> query = feedbackMapper.createLambdaQuery();
        if (StringUtil.isNotBlank(queryForm.getSearchWord())) {
            LambdaQuery<FeedbackEntity> subQuery = query.condition();
            subQuery.orLike(FeedbackEntity::getFeedbackContent, "%" + queryForm.getSearchWord() + "%");
            subQuery.orLike(FeedbackEntity::getUserName, "%" + queryForm.getSearchWord() + "%");
            query.and(subQuery);
        }

        if (queryForm.getStartDate() != null) {
            query.andGreatEq(FeedbackEntity::getCreateTime, DateUtil.toStartOfDay(queryForm.getStartDate()));
        }
        if (queryForm.getEndDate() != null) {
            query.andLessEq(FeedbackEntity::getCreateTime, DateUtil.toEndOfDay(queryForm.getEndDate()));
        }
        PageResult<FeedbackEntity> page = query.page(queryForm.getPageNo(), queryForm.getPageSize());
        PageResult<FeedbackVO> pageResult = SmartBeanUtil.copyPage(page, FeedbackVO.class);
        return ResponseDTO.ok(pageResult);
    }

    /**
     * 新建
     */
    public ResponseDTO<String> add(FeedbackAddForm addForm, RequestUser requestUser) {
        FeedbackEntity feedback = SmartBeanUtil.copy(addForm, FeedbackEntity.class);
        feedback.setUserType(requestUser.getUserType().getValue());
        feedback.setUserId(requestUser.getUserId());
        feedback.setUserName(requestUser.getUserName());
        feedbackMapper.insert(feedback);
        return ResponseDTO.ok();
    }
}
